package com.select.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.select.course.entity.SchoolCollege;
import com.select.course.entity.SchoolMajor;
import com.select.course.mapper.SchoolMajorMapper;
import com.select.course.service.SchoolCollegeService;
import com.select.course.service.SchoolMajorService;
import com.select.course.service.UserStudentService;
import com.select.course.utils.PageUtils;
import com.select.course.utils.R;
import com.select.course.vo.MajorVo;
import com.select.course.vo.PageVo;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 24820
* @description 针对表【c_school_major】的数据库操作Service实现
* @createDate 2022-10-26 23:08:18
*/
@Service
public class SchoolMajorServiceImpl extends ServiceImpl<SchoolMajorMapper, SchoolMajor>
    implements SchoolMajorService{

    @Resource
    private SchoolCollegeService schoolCollegeService;
    @Lazy
    @Resource
    private UserStudentService userStudentService;

    @Override
    public List<SchoolMajor> getMajorName() {
        List<SchoolMajor> list = this.list();
        return list;
    }

    @Override
    public List<MajorVo> getMajorByCollegeName(String name) {
        SchoolCollege name1 = schoolCollegeService.getOne(new QueryWrapper<SchoolCollege>().eq("name", name));
        List<SchoolMajor> schoolMajors = this.list(new QueryWrapper<SchoolMajor>().eq("college_id", name1.getId()));
        List<MajorVo> majorVos = schoolMajors.stream().map(e -> {
            MajorVo majorVo = new MajorVo();
            SchoolCollege id = schoolCollegeService.getOne(new QueryWrapper<SchoolCollege>().eq("id", e.getCollegeId()));
            majorVo.setCollege(id.getName());
            majorVo.setDescript(e.getDescript());
            majorVo.setRegisterTime(e.getRegisterTime());
            majorVo.setMajor(e.getName());
            majorVo.setId(e.getId());
            return majorVo;
        }).collect(Collectors.toList());
        return majorVos;

    }

    @Override
    public PageUtils getMajorPage(PageVo pageVo) {
        QueryWrapper<SchoolMajor> schoolMajorQueryWrapper = new QueryWrapper<>();
        Page<SchoolMajor> schoolMajorPage = new Page<>(pageVo.getPageNum(),pageVo.getPageSize());
        String college = pageVo.getCollege();
        if (!StringUtils.isEmpty(college)) {
            Map<String, Long> stringLongMap = userStudentService.releaseCollegeAndMajor(college, null, null);
            schoolMajorQueryWrapper.eq("college_id",stringLongMap.get("collegeId"));
        }
        String query = pageVo.getQuery();
        if (!StringUtils.isEmpty(query)) {
            schoolMajorQueryWrapper.and(e -> {
                e.like("id",query).or().like("name",query);
            });
        }
        Page<SchoolMajor> page = this.page(schoolMajorPage, schoolMajorQueryWrapper);
        List<SchoolMajor> records = page.getRecords();
        List<MajorVo> collect = records.stream().map(e -> {
            MajorVo majorVo = new MajorVo();
            Map<String, String> stringStringMap = userStudentService.packageCollegeAndMajor(e.getCollegeId(), e.getId(), null);
            majorVo.setCollege(stringStringMap.get("collegeName"));
            majorVo.setMajor(stringStringMap.get("majorName"));
            majorVo.setRegisterTime(e.getRegisterTime());
            majorVo.setId(e.getId());
            majorVo.setDescript(e.getDescript());
            return majorVo;
        }).collect(Collectors.toList());
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(collect);
        return  pageUtils;
    }

    @Transactional
    @Override
    public R addMajor(MajorVo majorVo) {
        SchoolMajor schoolMajor = new SchoolMajor();
        schoolMajor.setDescript(majorVo.getDescript());
        schoolMajor.setRegisterTime(new Date());
        schoolMajor.setName(majorVo.getMajor());
        SchoolCollege name = schoolCollegeService.getOne(new QueryWrapper<SchoolCollege>().eq("name", majorVo.getCollege()));
        schoolMajor.setCollegeId(name.getId());
        this.save(schoolMajor);
        return R.ok();
    }

    @Override
    public MajorVo getMajorInfo(String majorid) {
        SchoolMajor byId = this.getById(majorid);
        MajorVo majorVo = new MajorVo();
        majorVo.setDescript(byId.getDescript());
        Map<String, String> stringStringMap = userStudentService.packageCollegeAndMajor(byId.getCollegeId(), byId.getId(), null);
        majorVo.setCollege(stringStringMap.get("collegeName"));
        majorVo.setMajor(stringStringMap.get("majorName"));
        majorVo.setRegisterTime(byId.getRegisterTime());
        majorVo.setId(byId.getId());

        return majorVo;

    }

    @Transactional
    @Override
    public void deleteMjor(Long id) {
        this.removeById(id);
    }

    @Override
    public R updateMajor(MajorVo majorVo) {
        SchoolMajor schoolMajor = new SchoolMajor();
        Map<String, Long> stringLongMap = userStudentService.releaseCollegeAndMajor(majorVo.getCollege(), null, null);
        schoolMajor.setCollegeId(stringLongMap.get("collegeId"));
        schoolMajor.setId(majorVo.getId());
        schoolMajor.setDescript(majorVo.getDescript());
        schoolMajor.setName(majorVo.getMajor());
        this.updateById(schoolMajor);

        return R.ok();
    }
}




